//
//  QSNewFeatureViewController.m
//  SinaWeiBo
//
//  Created by apple on 16/6/4.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "QSNewFeatureViewController.h"
#import "QSNewFeatureViewCell.h"
#import "QSMainTabBarController.h"

@interface QSNewFeatureViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, QSNewFeatureViewCellDelegate>

/**
 *  分享
 */
@property (nonatomic, assign, getter=isShare) BOOL share;

@end

@implementation QSNewFeatureViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Register cell classes
    [self.collectionView registerClass:[QSNewFeatureViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // 添加代理
    self.collectionView.delegate = self;
}

#pragma mark - collectionViewCotroller的代理方法
/**
 *  cell即将显示
 */
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(QSNewFeatureViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 当是最后一张图片时，添加开始体验按钮
    if ((indexPath.item + 1) == kCount) {
        self.share = YES;
        // 添加按钮
        [cell.contentView addSubview:cell.startButton];
        [cell.contentView addSubview:cell.shareButton];
        // 对按钮进行自动布局
        [cell setupBtnLayout];
        [cell showStartButton];
        
        // 添加代理
        cell.delegate = self;
    }
}

#pragma mark - QSNewFeatureViewCell的代理方法
/**
 *  开始体验按钮的代理方法
 */
- (void)startBtnDelegateWithCell:(QSNewFeatureViewCell *)cell
{
    // 模态出QSMainTabBarController控制器
//    QSMainTabBarController *baseTarBarController = [[QSMainTabBarController alloc] init];
//    [self presentViewController:baseTarBarController animated:YES completion:nil];
    // 用通知实现跳转到主界面
    [[NSNotificationCenter defaultCenter] postNotificationName:AppSwitchRootViewControler object:nil];
}

/**
 *  分享按钮的代理方法
 */
- (void)shareBtnDelegateWithCell:(QSNewFeatureViewCell *)cell
{
    if (!self.share) {
        self.share = !self.share;
        [cell.shareButton setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateNormal];
    }else{
        self.share = !self.share;
        [cell.shareButton setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
    }
}

#pragma mark - collectionViewCotroller的数据源方法
/**
 *  多少组
 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

/**
 *  多少行
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return kCount;
}

/**
 *  每行显示的内容
 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 重用cell
    QSNewFeatureViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    // 隐藏开始体验按钮和分享按钮
    cell.startButton.hidden = YES;
    cell.shareButton.hidden = YES;
    
    // 设置cell的内容
    cell.imageIndex = indexPath.item;
    
    return cell;
}

/**
 *  重写init方法
 */
- (instancetype)init
{
    self = [super initWithCollectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    
    // 创建流水布局
    UICollectionViewLayout *flowLayout = nil;
    flowLayout = [self setupFlowLayout];
    
    // 注意，流水布局是加载collectionView上的而不是加载collectionViewController上的
    [self.collectionView initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    return self;
}

/**
 *  重写initWithCoder:方法(用sb或xib创建时调用)
 */
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    return self;
}

/**
 *  创建流水布局
 */
- (UICollectionViewLayout *)setupFlowLayout
{
    // 设置布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = [UIScreen mainScreen].bounds.size;    // 设置item的大小
    layout.minimumInteritemSpacing = 0; // 设置item之间的行间距
    layout.minimumLineSpacing = 0;  // 设置item之间的列间距
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; // 设置滚动方向为水平方向
    
    // 设置翻页效果
    self.collectionView.pagingEnabled = YES;
    
    // 设置水平滚动指示器不显示
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    // 设置没有弹簧效果
    self.collectionView.bounces = NO;

    return layout;
}

#pragma mark - 隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
    return YES;
}
@end
